<script lang="ts">
import { ref } from "vue";
import axios from 'axios';
import 'element-plus/theme-chalk/el-message.css';
//规则数据对象
const userinfo = ref({
	tel:"",
	pwd:'',
	agree:true
});
//规则数据对象
const rules = {
  tel: [
    { required: true, message: '账号不能为空',trigger: 'blur' },
    { min: 6, max: 24, message: '号码长度要求6-14个字符',trigger: 'blur' }
  ],
  pwd: [
    { required: true, message: '密码不能为空',trigger: 'blur' },
    { min: 6, max: 24, message: '密码长度要求6-14个字符',trigger: 'blur' }
  ],
  agree: [
    {
      validator: (_rule: any, val: any, callback: () => any) => {
        return val ? callback() : new Error('请先同意协议')
      }
    }
  ]
}
export default {
  data() {
    return {
      userinfo,
      rules,
      avatar:''
    }
  },
  methods: {
    
  async handleLogin() {
    try {
      const response = await axios.post('/egg/login', {
        tel: this.userinfo.tel,
        pwd: this.userinfo.pwd,
       // uid:this.userinfo.uid
      });
     console.log(response.data);
      // // 登录成功，处理返回的数据
      const { data } = response;
      // 根据后端返回的数据进行判断和处理
      if (data.success) {
        // 登录成功逻辑
        this.avatar=response.data.avatar//返回头像信息
        const userInfo = {
        uname: this.userinfo.uname, // 保存用户名
        tel: this.userinfo.tel, // 保存手机号
        pwd: this.userinfo.pwd // 保存密码
      };
      localStorage.setItem('userInfo', JSON.stringify(userInfo));
        // 将头像地址保存在本地存储中
        localStorage.setItem('userAvatar', this.avatar);
        // 可以根据需要进行页面跳转、保存登录状态等操作
        this.$router.push('/');
      } else {
        // 登录失败逻辑
        // 可以显示错误信息等
        this.$message.error(data.message);
      }
    } catch (error) {
      // 请求出错，处理错误逻辑
      console.error(error);
      this.$message.error('登录失败，请稍后重试');
    }
  }

  }
}

</script>
<template>
	<div class="allimg" >
    
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          站酷ZCOOL | 通行证
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
         <h2>站酷学习</h2>
        </nav>
        <div class="account-box">
            <div class="form">
      <el-form ref="formRef" :model="userinfo" :rules="rules" label-position="right" label-width="60px" status-icon>
        <el-form-item prop="tel" label="账户">
          <el-input v-model="userinfo.tel" />
        </el-form-item>
        <el-form-item prop="pwd" label="密码">
          <el-input v-model="userinfo.pwd" />
        </el-form-item>
        <el-form-item prop="agree" label-width="22px">
          <el-checkbox v-model="userinfo.agree" size="large">
            我已同意隐私条款和服务条款
          </el-checkbox>
        </el-form-item>
        <el-button size="large" class="subBtn" @click="handleLogin" >登录
    </el-button>
      </el-form>
    </div>
        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="">图片来自站酷海洛</a>
          <a href="">京ICP备11017824号-4 / 京ICP证130164号 北京市公安局朝阳分局备案编号:110105000501</a>
          <a href="">Copyright</a>
        </p>
        <p>Copyright © 2006-2023 ZCOOL 中文 English</p>
      </div>
    </footer>
 
</div>
</template>
 <style scoped lang='scss'>

.allimg{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('./img/2.jpg')
}
.login-header {
  background:#fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }
  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
    //   color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  height: 488px;
  position: relative;

  .wrapper {
	width: 336px;
    min-height: 400px;
    background: #fff;
    position: absolute;
    left: 450px;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
     // text-align: right;
      //align-items: center;
	  justify-content: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        // color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 60px;
     background: skyblue;

      &.disabled {
        background: #cfcdcd;
      }
    }
    .avatar {
  display: inline-block;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 2px solid #ccc;
  margin-right: 10px;
  vertical-align: middle;
}
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}
.subBtn {
  //background: $xtxColor;
  width: 100%;
  color: skyblue;
}
</style> -->